<template>
	<!-- 完成任务 -->
	<view class="building-add">
		<view style="padding-bottom: 140rpx;">
			<view class="add-content">
				<view class="add-item" v-if="type == 1">
					<view class="item-left">选择目标<text>*</text></view>
					<view class="item-right" @click="show=true">{{ target }}</view>
				</view>
				<view class="add-item" v-if="type == 2">
					<view class="item-left">目标任务<text>*</text></view>
					<view class="item-right">{{ name }}</view>
				</view>
				<view class="add-item">
					<view class="item-left">任务描述<text>*</text></view>
					<view class="item-right"><textarea v-model="description" placeholder-style="color:#999;font-size:26rpx" placeholder="请输入任务描述" ></textarea></view>
				</view>
			</view>
			<view class="add-head">
				<view class="head-left">
					<view class="head-left-1">上传图片</view>
					<view class="head-left-2">图片建议大小不超过2M</view>
				</view>
				<view class="head-right" @click="chooseImage">
					<image class="img" v-if="imgs==''" src="../../../static/icon/jia-.png"></image>
					<image class="img2" v-else :src="imgs"></image>
				</view>
			</view>
			<view class="video">上传视频</view>
			<view class="upload-content">
				<view class="con-1" v-if="flag"><image @click="chooseVideo" src="../../../static/icon/jia-.png"></image></view>
				<view class="con-2" v-else>
					<video :controls="false" :loop="true" :autoplay="true" :show-center-play-btn="false" show-fullscreen-btn="false" :src="videoUrl"></video>
				</view>
			</view>
			<view class="video">上传音频</view>
			<view>
			    <button @tap="startRecord">开始录音</button>
			    <button @tap="endRecord">停止录音</button>
			    <button @tap="playVoice">播放录音</button>
			</view>
		</view>
		
		 
		
		<view class="btn">
			<view class="btn-1" :class="btnFlag ? 'act2' : 'act1' " @click="handleSubmit">提交任务</view>
		</view>
		
		<u-toast ref="uToast" />
		<u-select v-model="show" :list="targetList" @confirm="confirmTarget"></u-select>
	</view>
</template>

<script>
	
	const recorderManager = uni.getRecorderManager();
	const innerAudioContext = uni.createInnerAudioContext();
	
	innerAudioContext.autoplay = true;
	
	export default{
		data() {
			return{
				targetType:0,
				text: 'uni-app',
				voicePath: '',
				show:false,
				targetList:[],
				target:'请选择',
				address:'',//
				longitude:'',//
				latitude:'',//
				targetId:'',//
				distributeId:'',//
				imgs:'',//
				description:'',//
				videoUrl:'',//
				src:'',
				voiceUrl:'',//
				btnFlag:true,
				flag:true,
				cameraList: [{
						value: 'back',
						name: '后置摄像头',
						checked: 'true'
					},
					{
						value: 'front',
						name: '前置摄像头'
					},
				],
				cameraIndex: 0,
				name:'',
				targetsId:'',
				type:0
			}
		},
		onLoad(options) {
			console.log(options)
			this.distributeId = options.id
			this.targetType = options.type
			this.targetsId = options.targetId
			this.name = options.name
			console.log(options)
			if(options.targetId == '' || options.targetId == undefined ) {
				this.type = 1
			} else {
				this.type = 2
			}
			
			// 音频
			let self = this;
			recorderManager.onStop(function (res) {
			    console.log('recorder stop' + JSON.stringify(res));
			    self.voicePath = res.tempFilePath;
				uni.uploadFile({
					url: self.$u.http.config.uplodeUrl,
					filePath: res.tempFilePath,
					name: 'file',
					success: (res) => {
						// console.log(JSON.parse(res.data))
						self.voiceUrl = JSON.parse(res.data).message
						uni.showToast({
							title:'音频上传成功',
							icon:'success'
						})
					},
					fail: (res) => {
						console.log(res)
						uni.showToast({
							icon:'none',
							title:'音频上传失败'
						})
					}
				})
			});
		},
		mounted() {
			this.targetList = []
			this.getAddress()
			// 获取目标ID
			if(this.targetType == 1) {
				console.log('人员')
				this.getResident()
			} else if (this.targetType == 2) {
				console.log('小区')
				this.getVillage()
			} else if (this.targetType == 3) {
				console.log('楼栋')
				this.getBuilding()
			} else if (this.targetType == 7) {
				console.log('店铺')
				this.getShop()
			} else if (this.targetType == 8) {
				console.log('企业')
				this.getCompany()
			} else if (this.targetType == 9) {
				console.log('学校')// ---
				this.getSchool()
			} else if (this.targetType == 10) {
				console.log('医院')// ---
				this.getHospital()
			} else if (this.targetType == 11) {
				console.log('公共设施')// ---
				this.getTargetListTran()
			}
		},
		methods:{
			startRecord() {
			    console.log('开始录音');
			
			    recorderManager.start();
			},
			endRecord() {
			    console.log('录音结束');
			    recorderManager.stop();
			},
			playVoice() {
			    console.log('播放录音');
			
			    if (this.voicePath) {
			        innerAudioContext.src = this.voicePath;
			        innerAudioContext.play();
			    }
			},
			// 确认目标
			confirmTarget(e) {
				console.log(e)
				this.target = e[0].label
				this.targetId = e[0].value
			},
			// 人员
			getResident() {
				let params = {
					pageNo:1,
					pageSize:2000000
				}
				this.$u.api.getResident(params)
					.then(res => {
						// console.log(res)
						res.result.records.forEach(item => {
							this.targetList.push({
								value:item.id,
								label:item.userName
							})
						})
					})
					.catch(err => {
						console.log(err)
						this.status = 'nomore'
						uni.showToast({
							icon:'none',
							title:'信号太差！'
						})
					})
			},
			// 小区
			getVillage() {
				this.$u.api.getVillage()
					.then(res => {
						// console.log(res)
						if (res.code == 200) {
							res.result.forEach(item => {
								this.targetList.push({
									value:item.id,
									label:item.name
								})
							})
						}
					})
					.catch(err => {
						console.log(err)
						uni.showToast({
							icon:'none',
							title:'请稍后再试'
						})
					})
			},
			// 楼栋
			getBuilding() {
				this.$u.api.getBuildingSearch()
					.then(res => {
						// console.log(res)
						if (res.code == 200) {
							res.result.forEach(item => {
								this.targetList.push({
									value:item.id,
									label:item.fullName
								})
							})
						}
					})
					.catch(err => {
						console.log(err)
						uni.showToast({
							icon:'none',
							title:'请稍后再试'
						})
					})
			},
			// 店铺
			getShop() {
				let params = {
					pageNo:1,
					pageSize:10000
				}
				this.$u.api.getCompany(params)
				.then(res => {
					// console.log(res)
					if (res.code == 200) {
						res.result.records.forEach(item => {
							this.targetList.push({
								value:item.id,
								label:item.name
							})
						})
					}
				})
				.catch(err => {
					console.log(err)
					this.status = 'nomore'
					uni.showToast({
						icon:'none',
						title:'请稍后再试'
					})
				})
			},
			// 企业
			getCompany() {
				let params = {
					pageNo:1,
					pageSize:2000
				}
				this.$u.api.pageEnterprise()
					.then(res => {
						// console.log(res)
						if(res.code == 200) {
							res.result.records.forEach(item => {
								this.targetList.push({
									value:item.id,
									label:item.name
								})
							})
						}
					})
					.catch(err => {
						console.log(err)
						uni.showToast({
							title:'获取企业失败',
							icon:'none'
						})
					})
			},
			// 学校
			getSchool() {
				let params = {
					pageNo:1,
					pageSize:2000
				}
				this.$u.api.getSchool(params)
					.then(res => {
						// console.log(res)
						if (res.code == 200) {
							res.result.records.forEach(item => {
								this.targetList.push({
									value:item.id,
									label:item.name
								})
							})
						}
					})
					.catch(err => {
						console.log(err)
						this.status = 'nomore'
						uni.showToast({
							icon:'none',
							title:'请稍后再试'
						})
					})
			},
			// 医院
			getHospital() {
				let params = {
					pageNo:1,
					pageSize:2000
				}
				this.$u.api.getHospital(params)
					.then(res => {
						// console.log(res)
						if (res.code == 200) {
							res.result.records.forEach(item => {
								this.targetList.push({
									value:item.id,
									label:item.neme
								})
							})
						}
					})
					.catch(err => {
						console.log(err)
						this.status = 'nomore'
						uni.showToast({
							icon:'none',
							title:'获取医院失败'
						})
					})
			},
			// 公共设施
			getTargetListTran() {
				this.$u.api.getTargetListTran()
					.then(res => {
						console.log(res)
						if(res.code == 200) {
							res.result.forEach(item => {
								this.targetList.push({
									value:item.id,
									label:item.name
								})
							})
						}
					})
					.catch(err => {
						console.log(err)
						uni.showToast({
							icon:'none',
							title:'获取设施失败'
						})
					})
			},
			
			// 地址
			getAddress() {
				let self = this;
				uni.getLocation({
					type: 'wgs84',
					geocode: true,
					success(res) {
						// console.log(res)
						self.longitude = res.longitude
						self.latitude = res.latitude
						self.address = res.address.province+res.address.city+res.address.district+res.address.street+res.address.streetNum
					},
					fail(err) {
						console.log(err)
						uni.showToast({
							title:'定位失败',
							icon:'none'
						})
					}
				})
			},
			// 上传图片
			chooseImage() {
				const self = this
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success(res) {
						// console.log(res.tempFilePaths[0])
						uni.uploadFile({
							url: self.$u.http.config.uplodeUrl,
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: (res) => {
								// console.log(JSON.parse(res.data))
								self.imgs = JSON.parse(res.data).message
							},
							fail: (res) => {
								console.log(res)
								uni.showToast({
									icon:'none',
									title:'上传失败'
								})
							}
						})
					}
				})
			},
			// 上传视频
			chooseVideo() {
				// 上传视频
				uni.chooseVideo({
					maxDuration:60,
					count: 1,
					camera: this.cameraList[this.cameraIndex].value,
					sourceType: ['album'],
					success: (responent) => {
						if(responent.tempFilePath.length != 0) {
							this.flag = false
						} else {
							this.flag = true
						}
						this.videoUrl = responent.tempFilePath
						// console.log(responent)
						uni.showLoading({
						    title: '视频正在上传'
						});
						uni.uploadFile({
							url: this.$u.http.config.uplodeUrl,
							method:"POST",
							filePath:this.videoUrl,
							name:'file',
							// formData: {
							//    biz: 'temp'
							// },
							success: (res) => {
								// console.log(res)
								uni.hideLoading()
								uni.showToast({
								    title: '视频上传成功',
								    duration: 1000
								});
								console.log(res)
								this.src = JSON.parse(res.data).message
								this.btnFlag = false
							},
							fail:res => {
								// console.log(res)
							}
						})
					},
					fail(err) {
						// console.log(err)
					}
				})
			},
			// 完成
			handleSubmit() {
				if(this.type == 1) {
					if(this.targetId == '') {
						return this.$refs.uToast.show({
							title: '请选择目标'
						})
					}
				}
				if(this.description == '') {
					return this.$refs.uToast.show({
						title: '请填写描述'
					})
				}
				if(this.imgs == '') {
					return this.$refs.uToast.show({
						title: '请上传图片'
					})
				}
				if(this.src == '') {
					return this.$refs.uToast.show({
						title: '请上传视频'
					})
				}
				if(this.voiceUrl == '') {
					return this.$refs.uToast.show({
						title: '请上传音频'
					})
				}
				if(this.btnFlag) {
					return this.$refs.uToast.show({
						title: '请等待视频上传完成'
					})
				}
				let params = {
					distributeId:this.distributeId,
					imgs:this.imgs,
					description:this.description,
					videoUrl:this.src,
					voiceUrl:this.voiceUrl,
					targetId:this.targetId==''?this.targetsId:this.targetId ,
					address:this.address,
					latitude:this.latitude,
					longitude:this.longitude
				}
				console.log(params)
				this.$u.api.submitTask(params)
					.then(res => {
						console.log(res)
						if(res.code == 200) {
							uni.showToast({
								title:'操作成功',
								icon:'success'
							})
							setTimeout(() => {
								uni.navigateBack({
									delta:1
								})
							},1500)
						}
					})
					.catch(err => {
						console.log(err)
						uni.showToast({
							title:'操作失败',
							icon:'none'
						})
					})
			}
		}

	}
</script>

<style lang="scss" scoped>
	.act1{
		background-color: #007AFF;
		color: #fff;
	}
	.act2{
		background-color: #cecece;
		color: #fff;
	}
	.building-add{
		background-color: #f5f6f7;
		min-height: 100vh;
		.gridcontent{
			height: 40vh;
			width: 100%;
			padding: 20rpx;
			color: #333;
			font-size: 27rpx;
			text-align: left;
			.item{
				width: 100%;
				padding: 5rpx 40rpx;
				overflow-y: auto;
				.item1{
					padding: 5rpx 40rpx;
					.item2{
						padding: 5rpx 40rpx;
						.item3{
							padding: 5rpx 40rpx;
							.item4{
								padding: 5rpx 40rpx;
								
							}
						}
					}
				}
			}
		}
		.video{
			padding: 20rpx;
			color: #000;
			font-size: 28rpx;
			font-weight: 600;
		}
		.upload-content{
			
			.con-1{
				margin: 30rpx auto;
				width: 85%;
				height: 700rpx;
				border-radius: 10rpx;
				border: 1px solid #f4f1f1;
				display: flex;
				justify-content: center;
				align-items: center;
				background-color: #cecece;
				image{
					width: 100rpx;
					height: 100rpx;
					
				}
			}
			.con-2{
				margin: 30rpx auto;
				width: 85%;
				height: 700rpx;
				video{
					border-radius: 10rpx;
					width: 100%;
					height: 100%;
					z-index: 100;
				}
			}
		}
		.add-head{
			display: flex;
			justify-content: space-between;
			align-items: center;
			background-color: #fff;
			height: 180rpx;
			padding: 20rpx;
			.head-left{
				.head-left-1{
					color: #000;
					font-size: 28rpx;
					font-weight: 600;
				}
				.head-left-2{
					color: #999;
					font-size: 24rpx;
					margin-top: 20rpx;
				}
			}
			.head-right{
				background-color: #cecece;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 10rpx;
				width: 150rpx;
				height: 150rpx;
				.img{
					width: 50%;
					height: 50%;
				}
				.img2{
					width: 100%;
					height: 100%;
				}
			}
		}
		.add-content{
			margin-top: 20rpx;
			.add-item{
				display: flex;
				// justify-content: space-between;
				align-items: flex-start;
				padding: 30rpx 20rpx;
				background-color: #fff;
				border-bottom: 1px solid #f4f1f1;
				.item-left{
					color: #333;
					font-size: 28rpx;
					flex: 2;
					text{
						color: #DD524D;
					}
				}
				.item-right{
					font-size: 27rpx;
					color: #999;
					display: flex;
					justify-content: flex-end;
					align-items: center;
					flex: 4;
					input{
						text-align: right;
					}
					input::placeholder{
					    font-size: 26rpx;
					    color: #999;
					}
					textarea{
						
					}
					text{
						display: inline-block;
						margin-left: 5rpx;
					}
				}
			}
		}
		.btn{
			width: 100%;
			height: 110rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #fff;
			font-size: 30rpx;
			background-color: #f4f1f1;
			position: fixed;
			bottom: 0;
			left: 0;
			border-top: 1px solid #CCE6FF;
			z-index: 1000;
			.btn-1{
				display: flex;
				justify-content: center;
				align-items: center;
				width: 600rpx;
				height: 80rpx;
				border-radius: 40rpx;
			}
			.btn-1:active{
				background-color: #CCE6FF;
			}
		}
	}
</style>
